package ui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class BusInquiry extends ManagerInterface{

        JFrame jf=new JFrame("公交路线查询");
        Object[] titles={"ID","途径站点","首站","末站"};
        Object[][] data={{"101路","S1->S2->S3->S4->S5","S1","S5"},
                {"102路","S2->S2->S3->S4->S6","S2","S6"},
                {"103路","S3->S2->S3->S4->S7","S3","S7"}};
        private Vector titlesV=new Vector();
        private Vector<Vector> dataV=new Vector<>();


        public void init(){
            for (int i = 0; i < titles.length; i++) {
                titlesV.add(titles[i]);
            }
            for (int i = 0; i < data.length; i++) {
                Vector t=new Vector<>();
                for (int j = 0; j < data[i].length; j++) {
                    t.add(data[i][j]);
                }
                dataV.add(t);
            }

            DefaultTableModel model=new DefaultTableModel(dataV,titlesV);
            JTable jTable=new JTable(model);
            //创建按钮
            JButton addRow=new JButton(("添加"));
            JButton deleteRow=new JButton("删除");
            JButton conserve=new JButton("保存");
            addRow.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    model.addRow(new Object[]{"",""});

                }
            });
            deleteRow.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int selectedRow = jTable.getSelectedRow();
                    model.removeRow(selectedRow);

                }
            });
            JPanel panel=new JPanel();
            panel.add(addRow);
            panel.add(deleteRow);
            panel.add(conserve);
            jf.add(panel, BorderLayout.SOUTH);
            //只能选择单行
            jTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            //设置列宽
            TableColumn column_1=jTable.getColumn(titles[1]);
            column_1.setMinWidth(80);
            TableColumn column_3=jTable.getColumn(titles[2]);
            column_3.setMaxWidth(50);
            TableColumn column_4=jTable.getColumn(titles[3]);
            column_4.setMaxWidth(50);
            jf.add(new JScrollPane(jTable));
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.pack();
            jf.setVisible(true);


        }
        /*  private class MyTableModel extends AbstractTableModel{

              @Override
              public int getRowCount() {
                  return dataV.size();
              }

              @Override
              public int getColumnCount() {
                  return titlesV.size();
              }

              @Override
              public Object getValueAt(int rowIndex, int columnIndex) {
                  return dataV.get(rowIndex).get(columnIndex);
              }
             public String getColumnName(int column){
                  return (String) titlesV.get(column);
             }

              @Override
              public boolean isCellEditable(int rowIndex, int columnIndex) {
                  return true;
              }
          }*/
        public static void main(String[] args) {
            new BusInquiry().init();



        }


}
